//
//  BuyNowCell.m
//  reSearchDemo
//
//  Created by 科技 on 15/10/9.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "BuyNowCell.h"
#import "UIImageView+WebCache.h"
@interface BuyNowCell() {
    UIImageView *IconImg;
    UILabel *nameLb;
    UIImageView *ProductImg;
    UILabel *ProductIntroductionLb;
    UILabel *MoneyLb;
    UILabel *NumberLb;
    UILabel *BuyNumberLb;
    UIButton *ReductionBtn;
    UIButton *AddBtn;
    UILabel *currentNumber;
    UIView *ProductBGView;
    
    int buyNum;
}

@end

@implementation BuyNowCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        IconImg = [[UIImageView alloc] init];
        IconImg.image = [UIImage imageNamed:@"Icon"];
        [self.contentView addSubview:IconImg];
        
        nameLb = [[UILabel alloc] init];
        [self.contentView addSubview:nameLb];
        
        ProductBGView = [[UIView alloc] init];
        [self.contentView addSubview:ProductBGView];
        
        ProductImg = [[UIImageView alloc] init];
        [ProductBGView addSubview:ProductImg];
        
        ProductIntroductionLb = [[UILabel alloc] init];
        [ProductBGView addSubview:ProductIntroductionLb];
        
        MoneyLb = [[UILabel alloc] init];
        [ProductBGView addSubview:MoneyLb];
        
        NumberLb = [[UILabel alloc] init];
        [ProductBGView addSubview:NumberLb];
        
        BuyNumberLb = [[UILabel alloc] init];
        [ProductBGView addSubview:BuyNumberLb];
        
        ReductionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:ReductionBtn];
        
        AddBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:AddBtn];
        
        currentNumber = [[UILabel alloc] init];
        [self.contentView addSubview:currentNumber];
        
        buyNum = 1;
        
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}

- (void)setModel:(GoodsCarList *)model {
    IconImg.frame = CGRectMake(10, 10, 20, 20);
    
    nameLb.frame = CGRectMake(CGRectGetMaxX(IconImg.frame) + 10, CGRectGetMinY(IconImg.frame), CGRectGetWidth(self.frame) - CGRectGetMaxY(IconImg.frame), 20);
    nameLb.text = model.shopname;
    nameLb.font = [UIFont systemFontOfSize:13];
    
    ProductBGView.frame = CGRectMake(0, CGRectGetMaxY(IconImg.frame) + 10, CGRectGetWidth(self.frame), 80);
    ProductBGView.backgroundColor = [UIColor lightGrayColor];
    
    ProductImg.frame = CGRectMake(CGRectGetMinX(ProductBGView.frame) + 10, 10, 60, 60);
    [ProductImg sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"背景.png"]];
    
    ProductIntroductionLb.frame = CGRectMake(CGRectGetMaxX(ProductImg.frame) + 10, 0, CGRectGetWidth(self.frame) - CGRectGetMaxX(ProductImg.frame) - 20, 40);
    ProductIntroductionLb.numberOfLines = 0;
    ProductIntroductionLb.font = [UIFont systemFontOfSize:13];
    ProductIntroductionLb.text = @"产品详细介绍";
    
    MoneyLb.frame = CGRectMake(CGRectGetMaxX(ProductImg.frame) + 10, CGRectGetMaxY(ProductImg.frame) - 10, 80, 20);
    MoneyLb.text = [NSString stringWithFormat:@"￥%.2f",model.money];
    MoneyLb.textColor = [UIColor orangeColor];
    
    NumberLb.frame = CGRectMake(CGRectGetWidth(self.frame) - 40, CGRectGetMaxY(ProductImg.frame) - 10, 40, 20);
    NumberLb.text = [NSString stringWithFormat:@"x%d",model.num];
    NumberLb.textAlignment = NSTextAlignmentCenter;
    
//    BuyNumberLb.frame = CGRectMake(10, CGRectGetMaxY(ProductBGView.frame) - 20, 100, 20);
//    BuyNumberLb.text = @"购买数量";
//    
//    ReductionBtn.frame = CGRectMake(CGRectGetWidth(self.frame) - 100, CGRectGetMinY(BuyNumberLb.frame) + 40, 30, 30);
//    [ReductionBtn setTitle:@"-" forState:UIControlStateNormal];
//    ReductionBtn.titleLabel.text = @"-";
//    ReductionBtn.backgroundColor = [UIColor lightGrayColor];
//    [ReductionBtn addTarget:self action:@selector(decreaseBtn:) forControlEvents:UIControlEventTouchUpInside];
//    
//    currentNumber.frame = CGRectMake(CGRectGetMaxX(ReductionBtn.frame), CGRectGetMinY(BuyNumberLb.frame) + 40, 30, 30);
//    currentNumber.text = @"1";
//    currentNumber.textAlignment = NSTextAlignmentCenter;
//    
//    AddBtn.frame = CGRectMake(CGRectGetMaxX(currentNumber.frame), CGRectGetMinY(BuyNumberLb.frame) + 40, 30, 30);
//    [AddBtn setTitle:@"+" forState:UIControlStateNormal];
//    AddBtn.titleLabel.text = @"+";
//    AddBtn.backgroundColor = [UIColor lightGrayColor];
//    [AddBtn addTarget:self action:@selector(addBtn:) forControlEvents:UIControlEventTouchUpInside];
    
}

/** 减号*/
- (void)decreaseBtn:(id)sender {
    if (buyNum>1) {
        buyNum--;
    }
    currentNumber.text = [NSString stringWithFormat:@"%d",buyNum];
    NumberLb.text = [NSString stringWithFormat:@"x%d",buyNum];
    self.block(buyNum);
}

/** 加号*/
- (void)addBtn:(id)sender {
    buyNum++;
    currentNumber.text = [NSString stringWithFormat:@"%d",buyNum];
    NumberLb.text = [NSString stringWithFormat:@"x%d",buyNum];
    self.block(buyNum);
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
